//
//  HomeViewController.swift
//  weibo
//
//  Created by tomato on 2021/9/8.
//

import UIKit

class HomeViewController: UIViewController {
   
    // MARK: - 懒加载属性
    private lazy var titleBtn:TitleButton = TitleButton()
    private lazy var statusArray:[StatusViewModel] = [StatusViewModel]()//数据
    var tableView: UITableView!
    // MARK: - 系统函数
    override func viewDidLoad() {
        super.viewDidLoad()
//        //访客视图
//        vistorView?.setupVisitorInfo(imageName:nil, message: "关注一些人,看看有什么惊喜!")
        //子view
        setupSubviews()
        //设置navigationBarItem
        setupNavigationBar()
        //加载数据
        requestData()
    }
}
// MARK: - 设置UI界面
extension HomeViewController{
    //设置导航栏内容
    private func setupNavigationBar(){
        // 1.左侧的Item
//        let rightItem = UIBarButtonItem(image:UIImage(named:"image_my_history"), style: .plain, target: self, action:#selector(rightItemClick))
//        navigationItem.rightBarButtonItem = rightItem
    }
    private func setupSubviews(){
        // 创建UITableView实例
        tableView = UITableView(frame: view.bounds, style: .plain)
        tableView.dataSource = self
        tableView.delegate = self
        //自适应高度设置
        tableView.estimatedRowHeight = 200
        tableView.rowHeight = UITableView.automaticDimension
        tableView.register(HomeCell.self, forCellReuseIdentifier: "HomeCell")
        view.addSubview(tableView)
    }
}
// MARK: - 事件监听
extension HomeViewController{
    //titleView点击事件
    @objc private func titleBtnClick(titleBtn:TitleButton){
        titleBtn.isSelected = !titleBtn.isSelected
    }
    //item点击事件
    @objc func rightItemClick(){
        let studyVC = StudyViewController()
        navigationController?.pushViewController(studyVC, animated:true)
    }
}
 
// MARK: - 加载数据
extension HomeViewController{
    //接口测试
    private func requestData(){
        NetworkAlamofireTools.sharedInstance.getHomeListWithLocalJson(fileName: "weibo_0") { (result) in
            guard let dataArray = result as? [Status] else {
                return
            }
            //转为viewModel
            for Status in dataArray{
                let viewModel = StatusViewModel(status:Status)
                self.statusArray.append(viewModel)
            }
            
            //刷新表格
            self.tableView.reloadData()
        } failure: { (error) in
            
        }
    }
}

// MARK: - tableview
extension HomeViewController:UITableViewDataSource, UITableViewDelegate {
    // 设置表格的行数
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.statusArray.count
    }
    // 设置每行的内容
     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
         let cell = tableView.dequeueReusableCell(withIdentifier: "HomeCell", for: indexPath) as! HomeCell
         let viewModel = self.statusArray[indexPath.row]
         cell.viewModel = viewModel
         return cell
    }
    // 点击某行时的响应方法
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
}








